import React, { useEffect, useState } from 'react';
import {
    Button,
    Cascader,
    DatePicker,
    Form,
    Input,
    InputNumber,
    Radio,
    Select,
    Switch,
    TreeSelect,
} from 'antd';
import TextArea from 'antd/es/input/TextArea';

// type SizeType = Parameters<typeof Form>[0]['size'];

const MyForm = (props) => {
    // 回显
    const [form] = Form.useForm()
    useEffect(() => {
        if (props.title == "编辑") {
            form.setFieldsValue(props.items)
        }

    }, [props.items])
    const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
        console.log('Success:', values);
        //     let option=[]
        //     if(values.role==1){
        //         option=[{value:'1',label:'超级管理员'}]
        //   }else{
        //         option=[{value:'2',label:'开发者'}]
        //   }
        if (props.title == "添加") {
            let obj = {
                ...values
            }
            props.data.push(obj)
        }else{
            let obj={
                
                ...props.items,
                ...values
            }
            let index=props.data.findIndex(item=>item.key==props.items.key)
            props.data[index]=obj
        }


        props.onCancel()
    }
    return (
        <Form
            form={form}
            key={props.key}
            onFinish={onFinish}
            style={{ maxWidth: 600 }}
        >
            <Form.Item label="用户名" name="name">
                <Input />
            </Form.Item>
            <Form.Item label="角色" name="role">
                <Select>
                    <Select key={props.role} options={
                        [
                            { value: '超级管理员', label: '超级管理员' },
                            { value: '开发者', label: '开发者' },
                        ]
                    } />
                </Select>
            </Form.Item>
            <Form.Item label="简介" name="desc">
                <TextArea />
            </Form.Item>
            <Form.Item label={null}>
                <Button type="primary" htmlType="submit">
                    确定
                </Button>
            </Form.Item>
        </Form>
    );
};

export default MyForm;